﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CalcTime
{
    class Program
    {
        static void Main(string[] args)
        {
            /* Pseudocode for Time Calculator
             * Display intro message to this program
             * input secNum seconds to be converted to minutes/hours/days
             * method timeCalc
             * method getAnswer
             * Display results
             */
            string loop = "Y";
            int totalMin = 0, totalDay = 0, totalHour = 0, totalWeek = 0, totalYear = 0;
            Int64 secNum = 0, remNum = 0;
            print("This program will convert any number of seconds given to either minutes, hours, days, weeks, months, or years");
            while (loop.ToUpper() == "Y")
            {
                getSeconds(ref secNum);
                timeCalc(ref totalMin, ref totalDay, ref totalHour, ref totalWeek, ref totalYear, ref remNum, secNum);
                string answer = "";
                getAnswer(ref answer, totalYear, totalWeek, totalDay, totalHour, totalMin, remNum);
                Console.WriteLine("{0} seconds are a total of {1}",secNum,answer);
                print("If you would like to input a different amount of seconds, type \"Y\" or type anything else to end the program");
                loop = Console.ReadLine();
            }
        }
        static void getAnswer(ref string answer, int totalYear, int totalWeek, int totalDay, int totalHour, int totalMin, long remNum)
        {
            if (totalYear >= 1)
            {
            answer = answer + totalYear + " Year(s)";
            }
            if (totalWeek >= 1)
            {
                answer = answer + totalWeek + " Week(s)";
            }
            if (totalDay >= 1)
            {
                answer = answer + totalDay + " Day(s)";
            }
            if (totalHour >= 1)
            {
                answer = answer + totalHour + " Hour(s)";
            }
            if (totalMin >= 1)
            {
                answer = answer + totalMin + " Minute(s)";
            }
            if (remNum > 0)
            {
                answer = answer + remNum + " Second(s)";
            }
        }
        static void timeCalc(ref int totalMin, ref int totalDay, ref int totalHour, ref int totalWeek, ref int totalYear, ref long remNum, long secNum)
        {
            remNum = secNum;
            totalYear = (int)remNum / 31449600; //get years, based on a 52 exact weeks per year
            remNum = remNum % 31449600;
            totalWeek = (int)remNum / 604800; // get weeks, based on 7 days
            remNum = remNum % 604800;
            totalDay = (int)remNum / 86400; //get days, based on 24 exact hours
            remNum = remNum % 86400;
            totalHour = (int)remNum / 3600;
            remNum = remNum % 3600;
            totalMin = (int)remNum / 60;
            remNum = remNum % 60;
        }
        static void getSeconds(ref long secNum)
        {
            print("Please provice the number of seconds to be converted:");
            while (!Int64.TryParse(Console.ReadLine(), out secNum))
            {
                print("Invalid number, provide whole numbers only, no decimals");
            }
        }
        static void print(string p)
        {
            Console.WriteLine("\n{0}",p);
        }
    }
}
